<?php
namespace Home\Controller;

use Think\Controller;
use Common\Service\Curl;
use Think\Log;
use Admin\Controller\QiniuController;
use Common\Service\Sms;
use Common\Service\MsgPush;

class TeacherController extends Controller
{

    public function __construct(){
        
    }

	/**
	 * 教师首页
	 */
	public function index(){

    	$userId = I('userId', '', 'trim');
    	$teacherId = I('teacherId', '', 'trim');
        $periodId = I('periodId', '', 'trim');

        if(!$userId){
        	$this->ajaxReturn(['status'=>false, 'code'=>1001, 'msg'=>'请登录']);
        }

        $map['teacher_id'] = $teacherId;

        if($periodId){
            $map['period'] = $periodId;
        }

        $tinfo = M("teacher")->where(['id'=>$teacherId])->find();

        $video_info = M('video')->where($map)->select();

        if(M('user_follow')->where(["user_id"=>$userId,"teacher_id"=>$teacherId])->find()){
        	$tinfo['isFollow'] = 1;
        }else{
        	$tinfo['isFollow'] = 0;
        }


        $tinfo["videoInfo"] = $video_info;

        $this->ajaxReturn(['status' => true, 'code'=>1000,'msg' => '查询成功','data'=>$tinfo]);

	}

	/**
	 * 推送
	 */
    public function msgpush(){
        $content = I('content', '', 'trim');

        $push = new MsgPush();

        if($content == ''){
            $this->ajaxReturn(['status' => false, 'code'=>1001,'msg' => '发送内容不能为空']);
        }

        $res = $push->sendNotifyAll($content);
        
        if($res){
            M('message')->add(["content"=>$content,'add_time'=>time()]);
        }

        echo '推送成功';
    }

    /**
     * 教师首页
     */
    public function message(){

        $userId = I('userId', '', 'trim');
        $page = I('page', '1', 'trim');
        $size = I('size', '5', 'trim'); 
        $firstNum = ($page-1)*$size;

        if(!$userId){
            $this->ajaxReturn(['status'=>false, 'code'=>1001, 'msg'=>'请登录']);
        }

        $res = M("message")->limit($firstNum,$size)->select();

        if($page == 1 && !$res){
            $this->ajaxReturn(['status'=>true, 'code'=>1000, 'msg'=>'无关注信息', 'data'=>[]]);
        }
    
        if($page > 1 && !$res){
            $this->ajaxReturn(['status'=>false, 'code'=>1002, 'msg'=>'无更多关注信息', 'data'=>[]]);
        }       

        $this->ajaxReturn(['status' => true, 'code'=>1000,'msg' => '查询成功','data'=>$res]);

    }
}